﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;

namespace dzs.Web.tenderExpert
{
    public partial class ListTotalTenderFirm : System.Web.UI.Page
    {
        Maticsoft.BLL.tenderFirmRecord bll = new Maticsoft.BLL.tenderFirmRecord();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #region gridView

        public void BindData()
        {
            if (Session["login"] != null)
            {
                DataSet ds_login = Session["login"] as DataSet;
                int uid = int.Parse(ds_login.Tables[0].Rows[0]["user_id"].ToString());
                DataSet ds = new DataSet();
                StringBuilder strWhere = new StringBuilder();
                strWhere.Append(" uid =" + uid + " and check_status = 1 ");
                if (txt_name.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and recordName like '%{0}%'", txt_name.Text.Trim());
                }

                ds = bll.GetList(strWhere.ToString());

                DataTable dt = ds.Tables[0].Clone();

                foreach (DataRow row in ds.Tables[0].Rows)
                {

                    DataRow r = dt.NewRow();

                    r["recordName"] = row["recordName"];
                    r["recordContent"] = row["recordContent"];
                    r["recordScore"] = row["recordScore"];
                    r["tenderTime"] = row["tenderTime"];
                    r["timeCount"] = row["timeCount"];

                    DateTime t2 = DateTime.Parse(row["tenderTime"].ToString());

                    DateTime t1 = DateTime.Now;

                    int day = Math.Abs(((TimeSpan)(t1 - t2)).Days);

                    int count = int.Parse(row["timeCount"].ToString());

                    if (day > count * 30)//如果时间超过规定月数
                    {
                        r["tenderScore"] = 0;
                    }
                    else
                    {
                        r["tenderScore"] = row["recordScore"];
                    }

                    dt.Rows.Add(r);


                }

                gridView.DataSource = dt;
                gridView.DataBind();
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void gridView_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_record = e.Row.FindControl("lbl_record") as Label;

                if (lbl_record.Text.Length > 10)
                {
                    lbl_record.Text = lbl_record.Text.Substring(0, 10) + "...";
                }

            }
        }

        protected void btn_out_Click(object sender, EventArgs e)
        {
            if (this.gridView.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "gb2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=totalTenderFirm.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.ContentType = "application/vnd.ms-excel";  
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>投标厂商行为记录</H4>");
                this.gridView.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
    }
}